package com.kkb.kkbportal.controller;


import com.kkb.kkbcommon.api.CommonResult;
import com.kkb.kkbportal.domain.Score;
import com.kkb.kkbportal.domain.dto.ScoreDto;
import com.kkb.kkbportal.service.ScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qtds
 * @since 2021-08-12
 */
@Api(tags = "评价管理", value = "评价模块")
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @ApiOperation(value = "添加评价", notes = "添加一个评价")
    @PostMapping
    public CommonResult<Object> postScore(@Validated @RequestBody ScoreDto scoreDto) {
        scoreService.addScore(scoreDto);
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation(value = "评价列表", notes = "显示简单的评价列表信息")
    @GetMapping("/list")
    public CommonResult<List<Score>> getListScore() {
        List<Score> list = scoreService.listAll();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "根据评价表主键id修改评价", notes = "根据评价表主键id修改评价")
    @ApiImplicitParam(name = "id", value = "评价id", required = true, dataType = "Long")
    @PutMapping("/{id}")
    public CommonResult<Object> putScoreById(@PathVariable Long id,
                                             @Validated @RequestBody ScoreDto scoreDto) {
        scoreService.updateById(id, scoreDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "根据评价人id,项目id,被评价人id修改评价")
    @PutMapping("")
    public CommonResult<Object> putScoreByMasterId(@Validated @RequestBody ScoreDto scoreDto) {
        scoreService.updateByMasterIdAndSlaveIdAndObjectId(scoreDto);
        return CommonResult.success(null, "修改成功");
    }

    @ApiOperation(value = "根据评价表主键id删除评价", notes = "根据评价表主键id删除评价")
    @ApiImplicitParam(name = "id", value = "评价表主键id", required = true, dataType = "Long")
    @DeleteMapping("/{id}")
    public CommonResult<Object> deleteScoreById(@PathVariable Long id) {
        scoreService.deleteById(id);
        return CommonResult.success(null, "删除成功");
    }

    /**
     * 返回被评价人的平均评分
     *
     * @param slaveId 被评价人id
     * @return 评价人评分
     */
    @ApiOperation(value = "返回被评价人的平均分", notes = "返回被评价人的平均分")
    @ApiImplicitParam(name = "slaveId", value = "被评价人的主键id", required = true, dataType = "Long")
    @GetMapping("/avg/slave/{slaveId}")
    public CommonResult<Object> countAvgSlaveStar(@PathVariable Long slaveId) {
        BigDecimal avgStar = scoreService.countAvgSlaveStar(slaveId);
        return CommonResult.success(avgStar);
    }

}

